/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.totoBD.gui.swing.components.dialog;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;

/**
 *
 * @author rgil
 */
public class DlgConUserDrop extends DlgConUserAbs implements ActionListener{
    public DlgConUserDrop(Frame parent, boolean modal) throws ClassNotFoundException{
        super("Drop an user",
              "Enter the name of the user to drop :",
              "Remove",
              parent,
              modal);
               javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTextBoxdef)
                    .addComponent(txfUserName)
                    .addComponent(lblUseDataBase)
                    .addComponent(txfDataBaseName, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .addComponent(cbxUseAllDataBases)
                    .addComponent(tmkGuiShrBtnGrpCommand, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap()
            )
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTextBoxdef)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txfUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUseDataBase)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txfDataBaseName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(cbxUseAllDataBases, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(tmkGuiShrBtnGrpCommand, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pack();
    }
    @Override
    protected boolean _doJob() throws Exception {
        String strUserName = super.txfUserName.getText();
        if (blnUseAllDataBases)
        {
            this._dbConnection_.executeUpdate(
            "REVOKE ALL PRIVILEGES ON *.* " +
            "FROM '" + strUserName + "'@'localhost'");
            this._dbConnection_.executeUpdate(
            "REVOKE GRANT OPTION ON *.* " +
            "FROM '" + strUserName + "'@'localhost'");
            this._dbConnection_.executeUpdate(
            "DELETE FROM mysql.user WHERE " +
            "User='" + strUserName + "' and Host='localhost'");
            this._dbConnection_.executeUpdate("FLUSH PRIVILEGES");
        }
        else
        {
            String strDatabse = this.txfDataBaseName.getText();
            this._dbConnection_.executeUpdate(
            "REVOKE ALL PRIVILEGES ON " + strDatabse + ".* " +
            "FROM '" + strUserName + "'@'localhost'");
            this._dbConnection_.executeUpdate(
            "REVOKE GRANT OPTION ON " + strDatabse + ".* " +
            "FROM '" + strUserName + "'@'localhost'");
            this._dbConnection_.executeUpdate(
            "DELETE FROM mysql.user WHERE " +
            "User='" + strUserName + "' and Host='localhost'");
            this._dbConnection_.executeUpdate("FLUSH PRIVILEGES");
        }

        return true;
    }
        @Override
    public void actionPerformed(ActionEvent e)
    {
       super.actionPerformed(e);
    }
}
